<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

/**
 * Function to display an icon from a given theme
 * This function will take icon from the path URL_RELATIVE/themes/$theme/$context/medias/icons/$size/$name
 * 
 * @param name : icon picture name, ex : home.png
 * @param size : icon size, ex : 16
 * @param theme : name from the theme to use, default is "default"
 * @param class : specify the class to assign to the icon, default is "icon".$size, ex : icon16, icon32
 * @param context : take the icon from the context folder, default will take "backend"
 * @param alt : the alternative text, default is the picture name
 * @param title : the alternative title, default is the picture name
 * 
 * @example {icon name="home.png" theme="original" context="frontend"}
 */
function smarty_function_icon($params, &$smarty)
{ 
	// === Default parameters
	$conf = f_loader_Config::getInstance()->getParameters("default");
	$default_theme = $conf['backTheme'];
	$default_size = "16";
	$default_context = "backend";
	$default_path = "%s/themes/%s/%s/medias/icons/%s/%s";
	
	// === check if picture name is given
	if(!isset($params['name']) || $params['name'] == "")
	{
		// no picture to display return nothing
		return "";
	}
	else
	{
		$name = $params['name'];	
	}
	
	// === get theme name
	if(!isset($params['theme']) || $params['theme'] == "")
	{
		$theme = $default_theme;
	}
	else
	{
		$theme = $params['theme'];
	}

	// === get context
	if(!isset($params['context']) || $params['context'] == "")
	{
		$context = $default_context;
	}
	else
	{
		$context = $params['context'];
	}

	// === get alt
	if(!isset($params['alt']) || $params['alt'] == "")
	{
		$alt = '';
	}
	else
	{
		$alt = $params['alt'];
	}	

	// === get title
	if(!isset($params['title']) || $params['title'] == "")
	{
		$title = '';
	}
	else
	{
		$title = $params['title'];
	}	
	
	// === get size
	if(!isset($params['size']) || $params['size'] == "")
	{
		$size = $default_size;
	}
	else
	{
		$size = $params['size'];
	}
	
	// === get class
	if(!isset($params['class']) || $params['class'] == "")
	{
		$class = "icon".$size;
	}
	else
	{
		$class = $params['class'];
	}	
	
	// === size is doubled because folders names are formatted like 1616, 3232, etc...
	if(strlen($size) == 2)
	{
		$size = $size.$size;
	}	
	
	// === get all other options
	$options = "";
	$notToTake = array("name", "size", "theme", "context", "class", "alt", "title");
	foreach($params as $key => $value)
	{
		if(!in_array($key, $notToTake))
		{
			$options .= $key.'="'.$value.'" ';
		}
	}

    $picture = sprintf($default_path, URL_RELATIVE, $theme, $context, $size, $name);
    
    if(isset($params['getOnlyPath']) && $params['getOnlyPath'] == 'true')
    {
        // Get only the relative path to the icon
        return $picture;
    }

	return '<img src="'.$picture.'" class="'.$class.'" alt="'.$alt.'" title="'.$title.'" '.$options.' />';
}